package com.suse.lhw.hms.foodserver.service.Impl;

import com.suse.lhw.hms.foodserver.dao.FFoodmodelMapper;
import com.suse.lhw.hms.foodserver.dao.FFoodmodelToFFoodmodelUnitMapper;
import com.suse.lhw.hms.foodserver.dao.FUnitMapper;
import com.suse.lhw.hms.foodserver.domin.FFoodmodel;
import com.suse.lhw.hms.foodserver.domin.FUnit;
import com.suse.lhw.hms.foodserver.pojo.output_POJO.FIAndName;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.suse.lhw.hms.foodserver.dao.FFoodmodelInstanceMapper;
import com.suse.lhw.hms.foodserver.domin.FFoodmodelInstance;
import com.suse.lhw.hms.foodserver.service.FFoodmodelInstanceService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.foodserver.service.Impl
 * @ClassName: FFoodmodelInstanceServiceImpl
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/4/18 19:30
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/18 19:30
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
public class FFoodmodelInstanceServiceImpl implements FFoodmodelInstanceService {

    @Resource
    private FFoodmodelInstanceMapper fFoodmodelInstanceMapper;
    @Resource
    private FUnitMapper fUnitMapper;
    @Resource
    private FFoodmodelToFFoodmodelUnitMapper fFoodmodelToFFoodmodelUnitMapper;
    @Resource
    private FFoodmodelMapper fFoodmodelMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return fFoodmodelInstanceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(FFoodmodelInstance record) {
        return fFoodmodelInstanceMapper.insert(record);
    }

    @Override
    public int insertSelective(FFoodmodelInstance record) {
        return fFoodmodelInstanceMapper.insertSelective(record);
    }

    @Override
    public FFoodmodelInstance selectByPrimaryKey(Integer id) {
        return fFoodmodelInstanceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(FFoodmodelInstance record) {
        return fFoodmodelInstanceMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(FFoodmodelInstance record) {
        return fFoodmodelInstanceMapper.updateByPrimaryKey(record);
    }

    @Override
    public int eatAFoodByModelId(int foodId, int unitId, int quanity, int eattime, String userId) {
        //添加一个foodmodelInstatce实例
        Date creatDate = new Date();
        //计算卡路里，先找出食品单位
        FUnit unit = fUnitMapper.selectByPrimaryKey(unitId);
        if (unit == null)
            System.out.println("没找到，好生反省");
        System.out.println("查找到食品单位为：" + unit.toString());
        //计算
        int calories = quanity * unit.getUnitConsumeCalories();
        FFoodmodelInstance instance = new FFoodmodelInstance(foodId, unitId, quanity, eattime, userId, creatDate, calories);
        if (fFoodmodelInstanceMapper.insertSelective(instance) == 0) {
            return 0;
        }
        return 1;
    }

    @Override
    public int splitOutAFoodByModelId(int instanceId) {
        //通过实例id删除实例
        return fFoodmodelInstanceMapper.deleteByPrimaryKey(instanceId);
    }

    @Override
    public List<FIAndName> selectByUserId(String userId) {
        List<FIAndName> fiAndNames = new ArrayList<FIAndName>();
        List<FFoodmodelInstance> fFoodmodelInstances = fFoodmodelInstanceMapper.selectByUserid(userId);
        for (FFoodmodelInstance instance : fFoodmodelInstances) {
            FFoodmodel fFoodmodel = fFoodmodelMapper.selectByPrimaryKey(instance.getFmId());
            FIAndName fiAndName = new FIAndName();
            fiAndName.setId(instance.getId());
            fiAndName.setFmId(instance.getFmId());
            fiAndName.setQuanity(instance.getQuanity());
            fiAndName.setType(instance.getType());
            fiAndName.setUserId(instance.getUserId());
            //格式化日期
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            fiAndName.setCreatetime(simpleDateFormat.format(instance.getCreatetime()));
            fiAndName.setCalories(instance.getCalories());
            fiAndName.setName(fFoodmodel.getName());
            fiAndName.setImgurl(fFoodmodel.getImgurl());
            fiAndNames.add(fiAndName);
        }
        return fiAndNames;
    }

    @Override
    public List<FFoodmodelInstance> selectByFoodModelId(int foodid) {

        return fFoodmodelInstanceMapper.selectByFoodId(foodid);
    }

}

